package Fisica.Precios;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import Fisica.Conexion;

public class FisicaPrecios {
	
	public static ResultSet rs;
	public static PreparedStatement st;
	public static Connection con;
	
	
	public static void close(ResultSet rs, PreparedStatement st, Connection con)
	{
	    if (rs!=null)
	    {
	        try
	        {   rs.close();
	        }catch(SQLException e)
	        { 	e.printStackTrace();
	        }
	    }
	    if (st != null)
	    {
	        try
	        {   st.close();
	        } catch (SQLException e)
	        {  	e.printStackTrace();
	        }
	    }
	    if (con != null)
	    {
	        try
	        {con.close();
	        } catch (SQLException e)
	        {	e.printStackTrace();
	        }
	    }
	}
	
	
	public static float getIVA (String cod_barra){
		float f;
		String sql = "select a.art_iva_ri, a.art_iva_rni "
				+ "from fac_articulos_barras as b, fac_articulos as a "
				+ "where b.art_barras = '"+cod_barra+"' and b.art_codigo = a.art_codigo;";
		try {
			con = Conexion.getConexion();
			st = con.prepareStatement(sql);	
			rs = st.executeQuery();
			
			boolean existencia =  rs.next();
			rs = st.executeQuery();	
			
			String iva21, iva105;
			if (existencia){
				rs.next();
				iva21 = rs.getString("art_iva_ri");
				iva105 = rs.getString("art_iva_rni");
				if (iva21.equals("S")){
					f = (float) 1.21;
				}else if (iva105.equals("S")){
							f = (float) 1.105;
						}else{
							f = (float) 1;
						}
			}else{
				f = -1;
			}
			close(rs, st, con);
			return f;
		} catch (SQLException e) {
			System.out.println("ERROR EN LA RECUPERACION DE DATOS DE LA BASE");
			e.printStackTrace();
		}
		return 0; 		
	}
	
//	select * from fac_precios where art_codigo = 000002 and art_barras = 0001111144447 and pre_lista = 1;
	public static float getPrecio(String cod_interno, int lista){
		float f;
		String sql = "select * from fac_precios where art_codigo = "+cod_interno+" and pre_lista = "+lista+";";
		try {
			con = Conexion.getConexion();					
			st = con.prepareStatement(sql);	
			rs = st.executeQuery();			
						
			if (rs.next()){				
				f = rs.getFloat("pre_precio");				
			}else{
				f = -1;
			}	
			close(rs, st, con);
			return f;
		} catch (SQLException e) {
			System.out.print("ERROR EN LA RECUPERACION DE DATOS DE LA BASE");
			e.printStackTrace();
		}
		return 0; 		
	}

	public static void main(String[] args) {
		FisicaPrecios p = new FisicaPrecios();
//		p.lisOperador();


	}

}
